import Vue from 'vue'
import VueRouter from 'vue-router'
import { Message } from 'element-ui'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'homePage',
    component: () => import(/* webpackChunkName: 'home' */'../components/home/home.vue'),
    children: [
      {
        path: '/',
        name: 'main',
        meta: {
          isOpen: false,
          canOpen: true,
          title: '常泰长江大桥CT-A4标工程沙盘-首页'
        },
        component: () => import(/* webpackChunkName: 'main' */'../views/main/index.vue')
      },
      {
        path: '/proj-info',
        name: 'projInfo',
        meta: {
          isOpen: false,
          title: '项目简介-常泰长江大桥CT-A4标工程沙盘'
        },
        component: () => import(/* webpackChunkName: 'projInfo' */'../views/projInfo/index.vue')
      },
      {
        path: '/image-progress',
        name: 'imageProgress',
        meta: {
          isOpen: false,
          title: '形象进度-常泰长江大桥CT-A4标工程沙盘'
        },
        component: () => import(/* webpackChunkName: 'imageProgress' */'../views/imageProgress/index.vue')
      },
      {
        path: '/intelligent-video',
        name: 'intelligentVideo',
        meta: {
          isOpen: false,
          title: '智能视频-常泰长江大桥CT-A4标工程沙盘'
        },
        component: () => import(/* webpackChunkName: 'intelligentVideo' */'../views/intelligentVideo/index.vue')
      },
      {
        path: '/people-grid',
        name: 'peopleGrid',
        meta: {
          isOpen: false,
          title: '人员网格-常泰长江大桥CT-A4标工程沙盘'
        },
        component: () => import(/* webpackChunkName: 'peopleGrid' */'../views/peopleGrid/index.vue')
      },
      {
        path: '/hydraulic-climbing',
        name: 'hydraulicClimbing',
        meta: {
          isOpen: false,
          title: '主塔液压爬模-常泰长江大桥CT-A4标工程沙盘'
        },
        component: () => import(/* webpackChunkName: 'hydraulicClimbing' */'../views/hydraulicClimbing/index.vue')
      },
      {
        path: '/reinforcement-generation',
        name: 'reinforcementGeneration',
        meta: {
          isOpen: false,
          title: '钢筋生产加工-常泰长江大桥CT-A4标工程沙盘'
        },
        component: () => import(/* webpackChunkName: 'reinforcementGeneration' */'../views/reinforcementGeneration/index.vue')
      },
      {
        path: '/mass-temperature-control',
        name: 'massTemperatureControl',
        meta: {
          isOpen: false,
          title: '大体积温控-常泰长江大桥CT-A4标工程沙盘'
        },
        component: () => import(/* webpackChunkName: 'massTemperatureControl' */'../views/massTemperatureControl/index.vue')
      },
      {
        path: '/steel-arch-rib',
        name: 'steelArchRib',
        meta: {
          isOpen: false,
          title: '钢拱肋吊装-常泰长江大桥CT-A4标工程沙盘'
        },
        component: () => import(/* webpackChunkName: 'steelArchRib' */'../views/steelArchRib/index.vue')
      },
      {
        path: '/road-plan',
        name: 'roadPlan',
        meta: {
          isOpen: true,
          canOpen: true,
          title: '施工规划-常泰长江大桥CT-A4标工程沙盘'
        },
        component: () => import(/* webpackChunkName: 'roadPlan' */'../views/roadPlan/index.vue')
      },
      {
        path: '/tower-one',
        name: 'tower-one',
        meta: {
          isOpen: true,
          canOpen: true,
          title: '1#塔式起重机-常泰长江大桥CT-A4标工程沙盘'
        },
        component: () => import(/* webpackChunkName: 'roadPlan' */'../views/towerManagement/index1.vue')
      },
      {
        path: '/tower-two',
        name: 'tower-two',
        meta: {
          isOpen: true,
          canOpen: true,
          title: '2#塔式起重机-常泰长江大桥CT-A4标工程沙盘'
        },
        component: () => import(/* webpackChunkName: 'roadPlan' */'../views/towerManagement/index2.vue')
      },
      {
        path: '/tower-three',
        name: 'tower-three',
        meta: {
          isOpen: true,
          canOpen: true,
          title: '3#塔式起重机-常泰长江大桥CT-A4标工程沙盘'
        },
        component: () => import(/* webpackChunkName: 'roadPlan' */'../views/towerManagement/index3.vue')
      },
      {
        path: '/tower-four',
        name: 'tower-four',
        meta: {
          isOpen: true,
          canOpen: true,
          title: '4#塔式起重机-常泰长江大桥CT-A4标工程沙盘'
        },
        component: () => import(/* webpackChunkName: 'roadPlan' */'../views/towerManagement/index4.vue')
      }
    ]
  },
  {
    path: '/login',
    name: 'login',
    meta: {
      title: '登录-常泰长江大桥CT-A4标工程沙盘'
    },
    component: () => import(/* webpackChunkName: 'login' */'../views/login/index.vue')
  },
  {
    path: '*',
    name: '404',
    meta: {
      title: 'notFound-常泰长江大桥CT-A4标工程沙盘'
    },
    component: () => import(/* webpackChunkName: '404' */'../views/404/index.vue')
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  if (to.name !== 'login' && to.name !== '404') {
    const lastTimer = localStorage.getItem('lastTimer')
    const expires = 5 * 24 * 60 * 60 * 1000

    if (
      localStorage.getItem('access_token') &&
      lastTimer &&
      Date.now() - lastTimer < expires
    ) {
      document.title = to.meta.title
      return next()
    } else {
      Message({
        type: 'error',
        message: '当前登录信息有误, 或Token已过期, 2s之后将自动跳转到登录页!'
      })

      const timer = setTimeout(() => {
        clearTimeout(timer)
        next({
          name: 'login'
        })
      }, 2000)
    }
  } else {
    document.title = to.meta.title
    next()
  }
})

export default router
